package com.haiheng.module_main.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.haiheng.common.api.API
import com.haiheng.common.constant.AppConstant
import com.haiheng.common.constant.Keys
import com.haiheng.common.fragment.RouterFragment
import com.haiheng.library.log.LogUtils
import com.haiheng.library.log.LogWriter
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.toast.ToastUtils
import com.haiheng.library_base.utils.SPUtils
import com.haiheng.module_main.databinding.SettingFragmentBinding

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/12/15 0015
 * @Version: 1.0
 */
class SettingFragment:RouterFragment(){
    private lateinit var binding: SettingFragmentBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = SettingFragmentBinding.inflate(inflater,container,false)
        return binding!!.root
    }
    override fun initView() {
        LogUtils.e("ddddddddddddddddd SettingFragment")
        binding.etBaseUrl.setText(API.getBaseUrl())
        binding.etAcsUrl.setText(API.getAcsUrl())
        binding.etWebviewUrl.setText(AppConstant.getWebViewUrl())
        binding.etLibraryCode.setText(AppConstant.getLibraryCode())
        binding.switchOfflineMode.isChecked = AppConstant.isOpenOfflineMode()
        binding.switchLog.isChecked = AppConstant.isOpenLog()
        binding.switchLog.setOnClickListener {
            binding.switchLog.isChecked =!AppConstant.isOpenLog()
            SPUtils.getInstance().put(Keys.SWITCH_OPEN_LOG,binding.switchLog.isChecked)
            LogWriter.setWriteFlag(AppConstant.isOpenLog())
        }
        binding.switchOfflineMode.setOnClickListener {
            binding.switchOfflineMode.isChecked = !AppConstant.isOpenOfflineMode()
            SPUtils.getInstance().put(AppConstant.SP_OPEN_OFFLINE_MODE, binding.switchOfflineMode.isChecked)
        }
        binding.llSave.setOnClickListener {
            var baseUrl = binding.etBaseUrl.text.toString()
            var acsUrl = binding.etAcsUrl.text.toString()
            var webViewUrl = binding.etWebviewUrl.text.toString()
            var libraryCode = binding.etLibraryCode.text.toString()

            if (webViewUrl.isNotBlank()){
                SPUtils.getInstance().put(AppConstant.SP_WEB_VIEW_URL,webViewUrl)
            }

            if (baseUrl.isNotBlank()){
                SPUtils.getInstance().put(API.SP_BASE_URL,baseUrl)
            }else{
                ToastUtils.showShort("上报地址不能为空")
                return@setOnClickListener
            }
            if (acsUrl.isNotBlank()){
                SPUtils.getInstance().put(API.SP_ACS_URL,acsUrl)
            }else{
                ToastUtils.showShort("acs地址不能为空")
                return@setOnClickListener
            }
            if (libraryCode.isNotBlank()){
                SPUtils.getInstance().put(AppConstant.SP_LIBRARYCODE,libraryCode)
            }
            ToastUtils.showShort("保存成功，退出程序，重新启动后生效")
        }
        binding.llExt.setOnClickListener {
            SPUtils.getInstance().put(AppConstant.SP_IS_EXT,true)
            activity.finish()
//            Process.killProcess(Process.myPid())
            System.exit(0)
        }
        binding.btnBack.setOnClickListener {
            activity.navigateUp()
        }

    }

    override fun initViewModel(): BaseViewModel? {
       return null
    }

    override fun initLoad() {

    }






}